/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.drawee;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.ResUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.oszc.bbhmlibrary.utils.LogUtil;
import com.oszc.bbhmlibrary.utils.MainHandler;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Switch;
import ohos.agp.components.element.Element;
import ohos.utils.net.Uri;

public class DraweeHierarchyAbility extends Ability {
    private final String TAG = DraweeHierarchyAbility.class.getSimpleName();
    private Component mBtLoadSuccess, mBtLoadFail, mBtClear;
    private Switch mSwhRetryEnable, mSwhRounded, mSwhNinePatch;
    private SimpleDraweeView mSimpleDrawee;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(com.facebook.fresco.samples.showcase.ResourceTable.Layout_layout_drawee_hierarchy);
        findViews();
        MainHandler.postDelay(this::loadData,2000);
        //loadData();
    }

    private void loadData() {
        LogUtil.error(TAG,"loadData");
        final Uri uriSuccess = ImageUriProvider.createSampleUri(
                ImageUriProvider.ImageSize.XL,
                ImageUriProvider.Orientation.ANY,
                ImageUriProvider.UriModification.CACHE_BREAKER);
        final Uri uriFailure = ImageUriProvider.getNonExistingUri();

        //VectorElement failureDrawable  = new VectorElement(this, ResourceTable.Graphic_ic_error_black_96dp);
        Element failureDrawable = ResUtil.getDrawable(this, ResourceTable.Media_icon);
        final ProgressBarDrawable progressBarDrawable = new ProgressBarDrawable();
        progressBarDrawable.setColor(ResUtil.getColor(this, ResourceTable.Color_accent));
        progressBarDrawable.setBackgroundColor(ResUtil.getColor(this, ResourceTable.Color_primary));
        progressBarDrawable.setRadius((int) ResUtil.getDimen(this, ResourceTable.Float_drawee_hierarchy_progress_radius));
        progressBarDrawable.setComponent(mSimpleDrawee);
        progressBarDrawable.setBounds(0,0,400,30);
        mSimpleDrawee.getHierarchy().setProgressBarImage(progressBarDrawable);
        mSimpleDrawee.getHierarchy().setFailureImage(failureDrawable, ScalingUtils.ScaleType.CENTER_INSIDE);
        mBtLoadSuccess.setClickedListener(view -> setUri(mSimpleDrawee, uriSuccess, mSwhRetryEnable.isChecked()));
        mBtLoadFail.setClickedListener(view -> setUri(mSimpleDrawee, uriFailure, mSwhRetryEnable.isChecked()));
        mBtClear.setClickedListener(view -> {
            mSimpleDrawee.setController(null);
            Fresco.getImagePipeline().evictFromCache(uriSuccess);
        });
        mSwhRounded.setCheckedStateChangedListener((buttonView, isChecked) -> {
            float corner = ResUtil.getDimen(this, ResourceTable.Float_drawee_hierarchy_corner_radius);
            LogUtil.error(TAG,"onStart corner:"+corner);
            RoundingParams roundingParams =
                    new RoundingParams().setCornersRadius(
                            isChecked ?  corner : 0);
            mSimpleDrawee.getHierarchy().setRoundingParams(roundingParams);
        });
        mSwhNinePatch.setCheckedStateChangedListener((buttonView, isChecked) -> {
            mSimpleDrawee
                    .getHierarchy()
                    .setPlaceholderImage(
                            isChecked ? ResourceTable.Media_ninepatch : ResourceTable.Media_logo,
                            isChecked ? ScalingUtils.ScaleType.FIT_XY : ScalingUtils.ScaleType.CENTER_INSIDE);
        });
    }

    private void findViews() {
        mBtLoadSuccess = findComponentById(ResourceTable.Id_load_success);
        mBtLoadFail = findComponentById(ResourceTable.Id_load_fail);
        mBtClear = findComponentById(ResourceTable.Id_clear);
        mSwhRetryEnable = (Switch) findComponentById(ResourceTable.Id_switch_retry_enabled);
        mSwhRounded = (Switch) findComponentById(ResourceTable.Id_switch_rounded);
        mSwhNinePatch = (Switch) findComponentById(ResourceTable.Id_switch_ninepatch);
        mSimpleDrawee = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee);
    }

    private void setUri(SimpleDraweeView draweeView, Uri uri, boolean retryEnabled) {
        draweeView.setController(
                Fresco.newDraweeControllerBuilder()
                        .setOldController(draweeView.getController())
                        .setTapToRetryEnabled(retryEnabled)
                        .setUri(uri)
                        .build());
    }
}
